package cn.ultrarisk.scadmin.manager.web.controller.stat;

import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrCollectionDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrNodeDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrReplicaDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrNode;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrReplica;
import cn.ultrarisk.scadmin.manager.biz.stat.dao.SolrNodeStatDAO;
import cn.ultrarisk.scadmin.manager.biz.stat.dao.SolrReplicaStatDAO;
import cn.ultrarisk.scadmin.manager.biz.stat.dto.HighchartResult;
import cn.ultrarisk.scadmin.manager.biz.stat.dto.StatisticsFilter;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * StatisticsController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/stat", produces = "application/json")
public class StatController extends BaseController {
    @Autowired
    private SolrCollectionDAO solrCollectionDAO;
    @Autowired
    private SolrNodeDAO solrNodeDAO;
    @Autowired
    private SolrReplicaDAO solrReplicaDAO;
    @Autowired
    private SolrNodeStatDAO solrNodeStatDAO;
    @Autowired
    private SolrReplicaStatDAO solrReplicaStatDAO;

    @ResponseBody
    @RequestMapping(value = "/nodes", method = RequestMethod.GET)
    public ResponseEntity<List<SolrNode>> findNodes(Integer collectionId) {
        return getOkResponseEntity(solrNodeDAO.findAvailable(collectionId));
    }

    @ResponseBody
    @RequestMapping(value = "/replicas", method = RequestMethod.GET)
    public ResponseEntity<List<SolrReplica>> findReplicas(Integer nodeId, Integer collectionId) {
        return getOkResponseEntity(solrReplicaDAO.findAvailable(nodeId, collectionId, true));
    }

    @ResponseBody
    @RequestMapping(value = "/qps", method = RequestMethod.GET)
    public ResponseEntity<Map<String, HighchartResult>> qps(StatisticsFilter filter) {
        Map<String, HighchartResult> result = new HashMap<String, HighchartResult>();
        result.put("avg", solrReplicaStatDAO.getAvgQpsStat(filter));
        result.put("max", solrReplicaStatDAO.getMaxQpsStat(filter));
        return getOkResponseEntity(result);
    }

    @ResponseBody
    @RequestMapping(value = "/visit", method = RequestMethod.GET)
    public ResponseEntity<HighchartResult> visit(StatisticsFilter filter) {
        return getOkResponseEntity(solrReplicaStatDAO.getVisitStat(filter));
    }

    @ResponseBody
    @RequestMapping(value = "/load", method = RequestMethod.GET)
    public ResponseEntity<Map<String, HighchartResult>> grow(StatisticsFilter filter) {
        Map<String, HighchartResult> result = new HashMap<String, HighchartResult>();
        result.put("avg", solrNodeStatDAO.getAvgLoadStat(filter));
        result.put("max", solrNodeStatDAO.getMaxLoadStat(filter));
        return getOkResponseEntity(result);
    }

    @ResponseBody
    @RequestMapping(value = "/grow", method = RequestMethod.GET)
    public ResponseEntity<HighchartResult> load(StatisticsFilter filter) {
        HighchartResult result = solrReplicaStatDAO.getGrowStat(filter);
        return getOkResponseEntity(result);
    }

    @ResponseBody
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public ResponseEntity<HighchartResult> status(StatisticsFilter filter) {
        HighchartResult result = solrReplicaStatDAO.getStatusStat(filter);
        return getOkResponseEntity(result);
    }

    @ResponseBody
    @RequestMapping(value = "/cache", method = RequestMethod.GET)
    public ResponseEntity<HighchartResult> cache(StatisticsFilter filter) {
        HighchartResult result = solrReplicaStatDAO.getCacheStat(filter);
        return getOkResponseEntity(result);
    }

    @ResponseBody
    @RequestMapping(value = "/memusage", method = RequestMethod.GET)
    public ResponseEntity<Map<String, HighchartResult>> memusage(StatisticsFilter filter) {
        Map<String, HighchartResult> result = new HashMap<String, HighchartResult>();
        result.put("avg", solrNodeStatDAO.getAvgMemusageStat(filter));
        result.put("max", solrNodeStatDAO.getMaxMemusageStat(filter));
        return getOkResponseEntity(result);
    }

}
